<?php defined('SYSPATH') or die('No direct script access.');

abstract class Model_Filter extends ORM
{  
    protected $_reload_on_wakeup = false;
    
    protected static function _compare($a, $b)
    {
        return $a['count'] < $b['count'];
    }
    
    protected static function _get_category_id($category, &$ids)
    {
        $category_id = $category; 
        if ($category) {
            if (is_array($category)) {
                $category_id = $category['id'];
                if ($category['childs']) {
                    $ids = explode(',', $category['childs']);    
                }
                $ids[] = $category['id'];    
            } else if (is_object($category)) {
                $category_id = $category->id;
                if ($category->childs) {
                    $ids = explode(',', $category->childs);    
                }
                $ids[] = $category->id;
            } else {
                $ids[] = $category;  
            }
        }
        return $category_id;    
    }
    
    protected static function _get_tag_id($tag)
    {
        $tag_id = $tag;
        if ($tag) {
            if (is_array($tag)) {
                $tag_id = $tag['id'];    
            } else if (is_object($tag)) {
                $tag_id = $tag->id;   
            }
        } 
        return $tag_id;      
    }
    
    protected static function _get_brand_ids($brands)
    {
        $brand_ids = array();
        if ($brands) {
            if (is_array($brands)) {
                $brand_ids[] = $brands['id'];    
            } else if (is_object($brands)) {
                $brand_ids[] = $brands->id;   
            } else if (is_string($brands)) {
                $brand_ids = explode(',', $brands);   
            }  
        } 
        return $brand_ids;  
    }
    
    protected static function _get_shop_ids($shops)
    {
        $shop_ids = array();
        if ($shops) {
            if (is_array($shops)) {
                $shop_ids[] = $shops['id'];   
            } else if (is_object($shops)) {
                $shop_ids[] = $shops->id;   
            } else if (is_string($shops)) {
                $shop_ids = explode(',', $shops);   
            } 
        }
        return $shop_ids;   
    }
    
    protected static function _get_from_price($price)
    {
        $from_price = null;
        if (isset($price['from']) && $price['from']) {
            $from_price = $price['from'];    
        }
        return $from_price;
    }
    
    protected static function _get_to_price($price)
    {
        $to_price = null;
        if (isset($price['to']) && $price['to']) {
            $to_price = $price['to'];    
        }
        return $to_price;    
    }
    
    protected static function _isset_brand_denormalized_data($category_id)
    {
        return true;    
    }
    
    protected static function _isset_shop_denormalized_data($category_id)
    {
        return true;    
    }
    
    protected static function _isset_category_denormalized_data($category_id)
    {
        return true;    
    }
    
    protected static function _get_brand_denormalized_data($category_id)
    {
        $data = DB::select('brand_id', 'count')
                    ->from('catalog_brand_filters')
                    ->where('category_id', '=', $category_id)
                    ->order_by('count', 'DESC')
                    ->limit(40)
                    ->execute()
                    ->as_array();

        return $data;    
    }
    
    protected static function _get_shop_denormalized_data($category_id)
    {
        $data = DB::select('shop_id', 'count')
                    ->from('catalog_shop_filters')
                    ->where('category_id', '=', $category_id)
                    ->execute()
                    ->as_array();

        return $data;    
    }
    
    protected static function _get_category_denormalized_data($category_id)
    {
        $data = DB::select(array('category_id2', 'category_id'), 'count')
                    ->from('catalog_category_filters')
                    ->where('category_id', '=', $category_id)
                    ->execute()
                    ->as_array();

        return $data;    
    }
    
    /**
    * Узнаем, есть ли в денормализованные данные для фильтров
    * Пока так - если это большая категория, то данные есть для всех пересечений фильтров
    * Если не большая - то только чистая категория и категория + 1 фильтр
    * TODO бренды без категории
    * 
    * @param mixed $category
    * @param mixed $brand
    * @param mixed $color
    * @param mixed $composition
    * @param mixed $season
    * @param mixed $shop
    * @param mixed $price
    */
    /**protected static function _isset_denormalized_data($category_id = null, $brand_id = null, $color_id = null, $composition_id = null, $season_id = null, $shop_id = null, $price = null)
    {
        $isset_data = false;
        
        //$big_category_ids = ORM::factory('category')->get_big_categories();
        if ($category_id) {
            //if (in_array($category_id, $big_category_ids)) {    // Большая категория
            //    $isset_data = true;    
            //} else {  
                $isset_price = (isset($price['from']) && $price['from'] && isset($price['to']) && $price['to']) ? 1 : 0;
                $sum = !is_null($brand_id) + !is_null($color_id) + !is_null($composition_id) + 
                       !is_null($season_id) + !is_null($shop_id) + $isset_price;
                if ($sum <= 1) {    // Установлен максимум один фильтр
                    $isset_data = true;    
                }   
            //} 
        } else {
            if ($brand_id) {
                // TODO    
            }
        }  
        
        return $isset_data;
    } */
    
    /**
    * Получить денормализованные данные в формате array(id => count, id => count ...)
    * 
    * @param mixed $category_id
    * @param mixed $brand_id
    * @param mixed $color_id
    * @param mixed $composition_id
    * @param mixed $season_id
    * @param mixed $shop_id
    * @param mixed $price
    */
    /*protected static function _get_denormalized_data($needed, $category_id = 0, $brand_id = 0, $color_id = 0, $composition_id = 0, $season_id = 0, $shop_id = 0, $price = null)
    {
        $category_id = $category_id ? $category_id : 0;
        $brand_id    = $brand_id ? $brand_id : 0;
        $color_id    = $color_id ? $color_id : 0;
        $composition_id = $composition_id ? $composition_id : 0;
        $season_id   = $season_id ? $season_id : 0;
        $shop_id     = $shop_id ? $shop_id : 0;
        
        $query = DB::select($needed, 'count')
                    ->from('catalog_filter_counts');
        if ($needed != 'category_id') {
            $query->where('base_category_id', '=', $category_id)
                  ->where('category_id', '=', 0);
        }
        if ($needed != 'brand_id') {
            $query->where('brand_id', '=', $brand_id);
        }
        if ($needed != 'color_id') {
            $query->where('color_id', '=', $color_id);
        }
        if ($needed != 'composition_id') {
            $query->where('composition_id', '=', $composition_id);
        } 
        if ($needed != 'season_id') {
            $query->where('season_id', '=', $season_id);
        }
        if ($needed != 'shop_id') {
            $query->where('shop_id', '=', $shop_id);
        }
        if ($needed != 'price') {
            $price_from = null;
            if (isset($price['from']) && $price['from']) {
                $price_from = $price['from'];    
            }
            $price_to = null;
            if (isset($price['to']) && $price['to']) {
                $price_to = $price['to'];    
            }
            
            $price = '';
            if ($price_from || $price_to) {
                $price = $price_from.'_'.$price_to;   
            }
            
            
            $query->where('price', '=', $price);
        }     
                    
        $data = $query
                    ->where($needed, '!=', ($needed == 'price') ? '' : 0)
                    ->execute()
                    ->as_array();

        return $data;  
    }   */
}